<template>
  <div class="index">
    <el-card class="box-card">
      <div class="block">
        <div>
          <span class="demonstration">活动时间</span>
          <el-date-picker
            v-model="began"
            type="datetime"
            placeholder="开始时间"
          >
          </el-date-picker>
          <el-date-picker
            v-model="closing"
            type="datetime"
            placeholder="结束时间"
          >
          </el-date-picker>
          <el-button type="primary" @click="search">搜索</el-button>
        </div>
        <el-button type="primary" @click="Addindex">添加</el-button>
      </div>

      
      <el-table :data="getCom" border style="width: 100%">
        <el-table-column fixed type="index" width="50" label="序号">
        </el-table-column>
        <el-table-column prop="date" label="创建日期">
          <template slot-scope="scope">
            {{ scope.row.date | Time }}
          </template>
        </el-table-column>
        <el-table-column prop="type" label="收支类型"> </el-table-column>
        <el-table-column prop="describe" label="收支描述"> </el-table-column>
        <el-table-column prop="income" label="收入"> </el-table-column>
        <el-table-column prop="expend" label="支出"> </el-table-column>
        <el-table-column prop="remark" label="描述"> </el-table-column>
        <el-table-column fixed="right" label="操作">
          <template slot-scope="scope">
            <el-button
              @click="handleClick(scope.row)"
              type="warning"
              size="small"
              >修改</el-button
            >
            <el-button type="danger" size="small" @click="open(scope.row._id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 添加弹出框 -->
    <el-dialog
      :title="editId ? '修改资金信息' : '添加资金信息'"
      :visible.sync="dialogFormVisible"
    >
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="活动区域" prop="type">
          <el-select v-model="ruleForm.type" placeholder="请选择收支类型">
            <el-option label="提现" value="提现"></el-option>
            <el-option label="提现手续费" value="提现手续费"></el-option>
            <el-option label="充值" value="充值"></el-option>
            <el-option label="优惠劵" value="优惠劵"></el-option>
            <el-option label="充值礼劵" value="充值礼劵"></el-option>
            <el-option label="转账" value="转账"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="收支描述" prop="describe">
          <el-input v-model="ruleForm.describe"></el-input>
        </el-form-item>

        <el-form-item label="收入" prop="income">
          <el-input v-model="ruleForm.income"></el-input>
        </el-form-item>

        <el-form-item label="支出" prop="expend">
          <el-input v-model="ruleForm.expend"></el-input>
        </el-form-item>

        <el-form-item label="账户现金" prop="cash">
          <el-input v-model="ruleForm.cash"></el-input>
        </el-form-item>

        <el-form-item label="备注" prop="remark">
          <el-input type="textarea" v-model="ruleForm.remark"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="resetForm('ruleForm')">取 消</el-button>
        <el-button type="primary" @click="submitForm('ruleForm')"
          >确 定</el-button
        >
      </div>
    </el-dialog>

    <!-- 分页 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page.sync="pageinfo.pagenum"
      :page-sizes="[5, 10, 15, 20]"
      :page-size.sync="pageinfo.pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>
  </div>
</template>
<script>
import { getNInformation, getAdd, getRem, getEdit } from "../utils/home";
import moment from "moment";
var token = localStorage.getItem("token");
export default {
  data() {
    return {
      began: "",
      closing: "",
      tableData: [],
      dialogFormVisible: false,
      ruleForm: {
        type: "",
        describe: "",
        income: "",
        expend: "",
        cash: "",
        remark: "",
      },
      rules: {
        describe: [
          { required: true, message: "收支描述不能为空", trigger: "blur" },
        ],
        income: [{ required: true, message: "收入不能为空", trigger: "blur" }],
        expend: [{ required: true, message: "支出不能为空", trigger: "blur" }],
        cash: [
          { required: true, message: "账户现金不能为空", trigger: "blur" },
        ],
      },
      editId: "", //保存修改id
      total: 1, //数据总数量
      pageinfo: {
        pagesize: 5, //每页数量
        pagenum: 1, //当前页数
      },
      tableData2: [],
    };
  },
  created() {
    this.getRequest();
  },
  filters: {
    Time(value) {
      moment.locale("zh-cn");
      return moment(value).format("YYYY-MM-DD hh:mm:ss");
    },
  },
  computed: {
    getCom () {
      return this.tableData.slice(
          (this.pageinfo.pagenum - 1) * this.pageinfo.pagesize,
          this.pageinfo.pagesize * this.pageinfo.pagenum
        );
    },
  },
  methods: {
    getRequest() {
      getNInformation().then((res) => {
        console.log(res);
        this.tableData = res
        // this.pageinfo.pagesize=this.tableData.length
        console.log(this.tableData);
        this.tableData2 = res
        this.total = res.length;
      });
    },
    search() {
      if (this.began && this.closing && new Date(this.began).getTime() <= new Date(this.closing).getTime()) {
        this.tableData = this.tableData2.filter((item, index) => {
          return (
            new Date(item.date).getTime() >= new Date(this.began).getTime() &&
            new Date(item.date).getTime() <= new Date(this.closing).getTime()
          );
        });
        console.log(this.tableData);
      } else if (!this.began && !this.closing) {
        //
        this.getRequest();
      } else {
        return this.$message({
          message: "请输入大于开始时间，并且小于结束时间",
          type: "success",
        });
      }
    },
    handleSizeChange(value) {
      console.log(value);
      this.pageinfo.pagesize = value;
      this.getRequest();
    },
    handleCurrentChange(value) {
      console.log(value);

      this.pageinfo.pagenum = value;
      this.getRequest();
    },
    handleClick(row) {
      this.editId = row._id;
      this.ruleForm = row;
      this.dialogFormVisible = true;
    },
    Addindex() {
      this.ruleForm = {
        type: "",
        describe: "",
        income: "",
        expend: "",
        cash: "",
        remark: "",
      };
      this.editId = "";
      this.dialogFormVisible = true;
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          // alert("submit!");
          // console.log(token);
          if (this.editId) {
            console.log(this.editId);
            getEdit({
              type: this.ruleForm.type,
              describe: this.ruleForm.describe,
              income: this.ruleForm.income,
              expend: this.ruleForm.expend,
              cash: this.ruleForm.cash,
              remark: this.ruleForm.remark,
              id: this.ruleForm._id,
            }).then((res) => {
              console.log(res);
              this.editId = "";
              this.dialogFormVisible = false;
              this.getRequest();
            });
          } else {
            getAdd(this.ruleForm, token).then((res) => {
              this.ruleForm = {
                type: "",
                describe: "",
                income: "",
                expend: "",
                cash: "",
                remark: "",
              };
              this.editId = "";
              this.dialogFormVisible = false;
              this.getRequest();
            });
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },

    open(id) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      });
      getRem({ id: id })
        .then((res) => {
          // console.log(res);
          this.getRequest();
        })
        .then(() => {
          this.$message({
            type: "success",
            message: "删除成功!",
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
  },
};
</script>
<style lang="scss" scoped>
.demonstration {
  margin-right: 15px;
}
.el-date-editor {
  margin-right: 6px;
}
.block {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 40px;
  margin-bottom: 20px;
}
</style>